<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/2/27-14:31
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\model\AdminUser;
    use app\admin\model\Menus;
    use app\admin\service\AdminUserService;
    use app\admin\service\RoleService;
    use Jrk\Redis;
    use Jrk\Trees;
    use Jrk\Util;

    class Admin extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model   = new AdminUser();
            $this->service = new AdminUserService();
            $this->del     = "管理员";
        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addUser
         * @describe:添加编辑
         */
        public function addUser()
        {
            if (IS_POST) {
                $data     = $this->request->post();
                $validate = new \app\admin\validate\AdminUser();
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }
                if (isset($data['repassword'])) {
                    unset($data['repassword']);
                }
                $data['password'] = Util::think_encrypt($data['password']);

                if (isset($data['id'])) {
                    addLog("修改管理员信息", "修改管理员：".$data['nickname']."信息");
                } else {
                    addLog("添加管理员", "添加管理员：".$data['nickname']);
                }

                return $this->model->addAndEdit($data);
            }

            $role = new RoleService();
            $da   = $role->getRoleList();
            $this->assign("role", $da);

            return $this->fetch();
        }


        public function myInfo()
        {

            return $this->fetch();
        }


        /**
         * @return mixed|\think\response\Json
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/3
         * @name: test
         * @describe:datatables
         */
        public function test()
        {
            //实例化User模型，注意要在上面use
            $UserModel = new Menus();
            //接受请求
            $datatables = request()->post();
            //得到排序的方式
            $order = $datatables['order'][0]['dir'];
            //得到排序字段的下标
            $order_column = $datatables['order'][0]['column'];
            //根据排序字段的下标得到排序字段
            $order_field = $datatables['columns'][$order_column]['data'];
            //得到limit参数
            $limit_start  = $datatables['start'];
            $limit_length = $datatables['length'];
            //得到搜索的关键词
            $search = $datatables['search']['value'];

            //查询出所有用户数据
            //如有搜索行为，则按照姓名进行模糊查询
            if ($search) {
                $data             = $UserModel->order("$order_field $order")->limit($limit_start, $limit_length)->where(
                    'title',
                    'LIKE',
                    "%$search%"
                )->select();
                $keyword_all_data = $UserModel->where('title', 'LIKE', "%$search%")->select();
                $total            = count($keyword_all_data);   //获取满足关键词的总记录数
            } else {

                //没有关键词，则查询全部
                $data = $UserModel->where('status', 1)//查询未被标记为删除的数据，可选
                ->field('id,title,href')->order("id $order")->limit($limit_start, $limit_length)->select();

                $total = $UserModel->count(); // 数据总数
            }

            if ($data) {
                $data = ($data)->toArray();
            }
            $draw = request()->post('draw');
            // dump($data);
            $AllData = [
                // ajax的请求次数，创建唯一标识
                'draw'            => $draw,
                // 结果数
                'recordsTotal'    => count($data),
                // 总数据量
                'recordsFiltered' => $total,
                // 总数据
                'data'            => $data,
            ];

            return json($AllData);


        }


        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: editUser
         * @describe:编辑
         */
        public function editUser()
        {
            $role             = new RoleService();
            $roles            = $role->whereNotIn("id", "1")->selectOrFail();
            $id               = $this->request->param("id/d");//父id
            $info             = $this->service->getOne($id);
            $info['password'] = Util::think_decrypt($info['password']);
            $this->assign(compact("info", "roles"));

            return $this->fetch();
        }



        /**
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: tree
         * @describe:获取tree
         */
        public function tree()
        {
            if (IS_AJAX) {
                $role = new RoleService();
                $da   = $role->getRoleList();
                if (!empty($da)) {
                    foreach ($da as $k => $v) {
                        $da[$k]['title'] = $v['name'];
                    }
                    $data = Trees::DeepTree($da);
                } else {
                    $data = [];
                }

                $this->ajaxResult($data, 100, '');
            }
        }



    }